<template>
    <div>
        用户名：<input v-model="user.username"/>
        密码:<input v-model="user.password"/>
        <button @click="login">登录</button>
        <div v-show="isErr">用户名或者密码错误</div>
    </div>
</template>

<script >
    export default {
        name: "login",
        data(){
            return{
                user:{
                    username:'',
                    password:''
                },
                isErr:false
            }
        },methods:{
            login(){
                this.axios({
                    url:'http://localhost:3000/users/login',
                    data:this.user,
                    method:'post',
                    dataType:'json'
                }).then((res)=>{
                    console.log(res.data);
                     if(res.data.flag){
                         this.$router.push({
                             path:'/'
                         }); // 跳转主页


                          console.log(res.data.user);
                         this.$store.commit('setUser',res.data.user);
                         alert(this.$store.getters.user.role.roleId);
                     }else {
                         this.isErr=true;
                     }
                })
            }
        }
    }
</script>

<style scoped>

</style>
